﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace MesgessSystem.ApplicationInstance
{
    public partial class OpenSaveApplication : XtraForm
    {
        public OpenSaveApplication()
        {
            InitializeComponent();
        }

        private void OpenSaveApplication_Load(object sender, EventArgs e)
        {
            btnOpen.Enabled = true;
            btnSave.Enabled = true;
        }
        /// <summary>
        /// 打开保存文本对话框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpen_Click(object sender, EventArgs e)
        {
            openFileDialog.FileName = "";
            openFileDialog.Filter = "RTF File(*.rtf)|*.RTF|TXT FILE(*.txt)|*.txt";
            openFileDialog.ShowDialog();
            if (openFileDialog.FileName != "")
            switch (openFileDialog.FilterIndex)            
            {
                    case 1:    //选择的是.rtf类型
                        richTextBox1.LoadFile(openFileDialog.FileName, RichTextBoxStreamType.RichText);
                        break;
                    case 2:   //选择的是.txt类型
                        richTextBox1.LoadFile(openFileDialog.FileName, RichTextBoxStreamType.PlainText);
                        break;
            }
            btnSave.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            saveFileDialog.Filter = "RTF File(*.rtf)|*.RTF|TXT FILE(*.txt)|*.txt";
            try
            {
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    switch (openFileDialog.FilterIndex)
                    {
                        case 1:    //选择的是.rtf类型
                            richTextBox1.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.RichText);
                            break;
                        case 2:   //选择的是.txt类型
                            richTextBox1.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);
                            break;
                    }
                XtraMessageBox.Show("保存成功！");
            }
            catch (Exception)
            {
                XtraMessageBox.Show(e.ToString());
            }          
        }
        ///// <summary>
        ///// 引发 <see cref="E:System.Windows.Forms.Control.KeyUp"/> 事件。
        ///// </summary>
        ///// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.KeyEventArgs"/>。</param>
        //protected override void OnKeyUp(KeyEventArgs e)
        //{
        //    base.OnKeyUp(e);
        //    if (e.Control && e.KeyCode == Keys.T)
        //    {
        //        LookSourceCode();
        //    }
        //}
        private void btnLook_Click(object sender, EventArgs e)
        {
            lookSource ls = new lookSource();
            ls.ShowDialog();
        }
        //string str = "";
        ///// <summary>
        ///// 查看源码
        ///// </summary>
        //private void LookSourceCode()
        //{
        //    StringBuilder sb = new StringBuilder();
        //    sb.AppendFormat("{0}",str);
        //    sb.AppendLine();
        //    XtraMessageBox.Show(sb.ToString());
        //}     
    }
}
